package com.yly.doctorapp.ui.order;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yly.doctorapp.R;
import com.yly.doctorapp.adapter.OrderAdapter;
import com.yly.doctorapp.adapter.OrderXialaAdapter;
import com.yly.doctorapp.base.BaseActivity;
import com.yly.doctorapp.base.DoctorAppApplication;
import com.yly.doctorapp.bean.OrderFirstBean;
import com.yly.doctorapp.bean.OrederBean;
import com.yly.doctorapp.bean.ResponsDataBean;
import com.yly.doctorapp.constant.Configution;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderListActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ll_type)
    LinearLayout llType;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.lv_order)
    ListView lvOrder;
    @BindView(R.id.lv_xiala)
    ListView lvXiala;
    @BindView(R.id.ll_xiala)
    LinearLayout llXiala;
    private List<OrederBean> mOrederList = new ArrayList<>();
    private OrderAdapter adapter;
    private List<OrederBean> orederBeanList;
    private List<String> orederxialList = new ArrayList<>();
    private OrderXialaAdapter xialaAdapter;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_orderlist;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        adapter = new OrderAdapter(OrderListActivity.this, mOrederList);
        lvOrder.setAdapter(adapter);
        for (int i = 0; i < 6; i++){
            orederxialList.add("" + (i - 1));
        }
        xialaAdapter = new OrderXialaAdapter(this, orederxialList);
        xialaAdapter.setSelectPosition(0);
        lvXiala.setAdapter(xialaAdapter);
        llXiala.getBackground().setAlpha(100);
        lvXiala.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                llXiala.setVisibility(View.GONE);
                xialaAdapter.setSelectPosition(position);
                xialaAdapter.notifyDataSetChanged();
                setType(position - 1);
                if (null != orederBeanList && orederBeanList.size() > 0){
                    mOrederList.clear();
                    getType((position - 1));
                    adapter.notifyDataSetChanged();
                    //在这里计算总金额
                    calumniateMoney();
                }
            }
        });
        adapter = new OrderAdapter(OrderListActivity.this, mOrederList);
        lvOrder.setAdapter(adapter);
    }

    private void setType(int index) {
        switch (index){
            case -1:
                tvType.setText("全部");
                break;
            case 0:
                tvType.setText("普通产品订单");
                break;
            case 1:
                tvType.setText("中医知识产品订单");
                break;
            case 2:
                tvType.setText("医生推荐产品订单");
                break;
            case 3:
                tvType.setText("咨询费订单");
                break;
            case 4:
                tvType.setText("处方订单");
                break;
        }
    }

    private void calumniateMoney() {
        if (null != mOrederList && mOrederList.size() > 0){
            double money = 0;
            for (int i = 0; i < mOrederList.size(); i++){
                money = money + mOrederList.get(i).getLatestPrice();
            }
            tvMoney.setText("¥" + money);
        }else {
            tvMoney.setText("¥0");
        }
    }

    @Override
    protected void initData() {
        if (null != DoctorAppApplication.mUserBean){
            initGetOrderList(DoctorAppApplication.mUserBean.getDoctorid());
        }
    }

    private void initGetOrderList(int doctorid) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.QUERYORDERITEM);
        params.addBodyParameter("doctorId", "" + doctorid);
        params.addBodyParameter("flag", "-1");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mOrederList.clear();
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)){
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")){
                        if (!StringUtils.isEmpty(responsDataBean.getData())){
                            OrderFirstBean orderFirstBean = JSON.parseObject(responsDataBean.getData(), OrderFirstBean.class);
                            if (!StringUtils.isEmpty(orderFirstBean.getMapList())){
                                orederBeanList = JSON.parseArray(orderFirstBean.getMapList(), OrederBean.class);
                                if (null != orederBeanList && orederBeanList.size() > 0){
                                    mOrederList.addAll(orederBeanList);
                                    calumniateMoney();
                                }
                            }
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接失败", 100);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getType(int positon){
        switch (positon){
            case -1:
                mOrederList.addAll(orederBeanList);
                break;
            case 0:
                getMyOrderList(0);
                break;
            case 1:
                getMyOrderList(1);
                break;
            case 2:
                getMyOrderList(2);
                break;
            case 3:
                getMyOrderList(3);
                break;
            case 4:
                getMyOrderList(4);
                break;
        }
    }

    private void getMyOrderList(int positon) {
        for (int i = 0; i < orederBeanList.size(); i++){
            if (orederBeanList.get(i).getFlag() == positon){
                mOrederList.add(orederBeanList.get(i));
            }
        }
    }

    @OnClick({R.id.iv_back, R.id.ll_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_type:
                llXiala.setVisibility(View.VISIBLE);
                xialaAdapter.notifyDataSetChanged();
                break;
        }
    }
}
